<?php

class HoteldatesController extends CController
{
	public function actionIndex()
	{
		$hotelId = intval($_GET['id']);
		
		$this->setPageTitle('Отели');
		
		$hotel = Hotel::model()->findByPk($hotelId);
		
		$this->render('index', array(
			'hotelId' => $hotelId,
			'hotel'   => $hotel,
			'maxDateFields' => 8 
		));
	}
	
	public function actionGetdata()
	{
		$hotelId = intval($_POST['hotelId']);
		
		$records = HotelDateGroup::model()->criteria()->with(array('hotelDates'))->findAll('hotelId=:hotelId', array(':hotelId' => $hotelId));

		$this->renderPartial('json', array(
			'data'  => $records
		), false, true);
	}
	
	public function actionGroupadd()
	{
		$hotelId = intval($_GET['hotelId']);
		
		$hotelDateGroup = new HotelDateGroup();
		$hotelDateGroup->attributes = array('hotelId' => $hotelId);
			
		if ($hotelDateGroup->save()) {
			for ($i = 0; $i < count($_POST['dateStart']); $i++) {
				if ($_POST['dateStart'][$i] != '' && $_POST['dateEnd'][$i] != '') {
					$hotelDate = new HotelDate();

					$hotelDate->hotelDateGroupId = $hotelDateGroup->id;
					
					$tmpArr = explode('/', $_POST['dateStart'][$i]);
   					$hotelDate->dateStart = $tmpArr[2] . '-' . $tmpArr[1] . '-' . $tmpArr[0];
   					$tmpArr = explode('/', $_POST['dateEnd'][$i]);
   					$hotelDate->dateEnd = $tmpArr[2] . '-' . $tmpArr[1] . '-' . $tmpArr[0];

					$hotelDate->save();
				}
			}
		}
		
		die('{"success":true,"msg":"ok"}');
	}
	
	public function actionGroupdelete()
	{
		$id = intval($_GET['id']);
		
		HotelDateGroup::model()->deleteByPk($id);
		HotelDate::model()->deleteAll('hotelDateGroupId=:hotelDateGroupId', array(':hotelDateGroupId' => $id));
		HotelRoomAndDates::model()->deleteAll('hotelDateGroupId=:hotelDateGroupId', array(':hotelDateGroupId' => $id));
		
		die();
	}
	
	public function actionAdd()
	{
		$hotelId = intval($_GET['hotelId']);
		$groupId = intval($_GET['groupId']);
		
		for ($i = 0; $i < count($_POST['dateStart']); $i++) {
			if ($_POST['dateStart'][$i] != '' && $_POST['dateEnd'][$i] != '') {
				$hotelDate = new HotelDate();

				$hotelDate->hotelDateGroupId = $groupId;
					
				$tmpArr = explode('/', $_POST['dateStart'][$i]);
				$hotelDate->dateStart = $tmpArr[2] . '-' . $tmpArr[1] . '-' . $tmpArr[0];
   				$tmpArr = explode('/', $_POST['dateEnd'][$i]);
   				$hotelDate->dateEnd = $tmpArr[2] . '-' . $tmpArr[1] . '-' . $tmpArr[0];

				$hotelDate->save();
			}
		}
		
		die('{"success":true,"msg":"ok"}');
	}
	
	public function actionDelete()
	{
		$id  = intval($_GET['id']);
		$row = HotelDate::model()->findByPk($id);
		
		if ($row) {
			HotelDate::model()->deleteByPk($id);
			
			$count = HotelDate::model()->count('hotelDateGroupId=:hotelDateGroupId', array(':hotelDateGroupId' => intval($row->hotelDateGroupId)));
			
			if (intval($count) == 0) {
				HotelDateGroup::model()->deleteByPk($row->hotelDateGroupId);
				HotelRoomAndDates::model()->deleteAll('hotelDateGroupId=:hotelDateGroupId', array(':hotelDateGroupId' => $row->hotelDateGroupId));
			}
		}
		
		die();
	}
}